/*
 * Project: Daycare Web Application
 * Package: com.madetoordersoftware.daycare.business
 * File:    ReportBuilder
 * Created: Dec 28, 2013
 */

/*
 *  Copyright (c) 2010 Made 2 Order Software, LLC
 *
 *  All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 *  transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 *  You may not distribute the URL or redistribute the files to third parties. You may transfer the files
 *  onto a single storage device so long as you can prove ownership of the software. You may not reverse
 *  engineer, decompile, or disassemble the software or media files. You may not modify or translate the
 *  software or media, or distribute copies of the software or media without the written consent of Made
 *  2 Order Software
 */
package com.made2ordersoftware.daycare.business.report;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.made2ordersoftware.daycare.domain.TimeWatchedEntity;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import javax.ejb.Stateless;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Stateless
public class ReportBuilder {

    private static final Logger LOG = LoggerFactory.getLogger(ReportBuilder.class);

    private final IReport report;

    /**
     * Creates an instance of <code>ReportBuilder</code> with a default report type of Date.
     */
    public ReportBuilder() {
        report = new DateReport();
    }

    /**
     * Creates an instance of <code>ReportBuilder</code>.
     *
     * @param reportType Type of report being generated.
     */
    public ReportBuilder(IReport reportType) {
        this.report = reportType;
    }

    /**
     * Creates a pdf document to be used for display.
     *
     * @param records Records to display in pdf file.
     *
     * @return The pdf dcoument
     *
     * @throws DocumentException     Any exception thrown while created the document.
     * @throws FileNotFoundException Thrown if the document cannot be found.
     */
    public Document buildReport(List<TimeWatchedEntity> records) throws DocumentException,
            FileNotFoundException {
        LOG.info("Inside displayReport");
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("daycareReport.pdf"));
        document.open();
        document.add(report.fillInTable(records));
        document.close();
        return document;
    }

}
